package com.neptune.imapp.ui.compose.screen.mainPage.page

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.scale
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.Dialog
import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.HorizontalPager
import com.google.accompanist.pager.VerticalPager
import com.google.accompanist.pager.rememberPagerState
import com.neptune.imapp.R
import com.neptune.imapp.repository.entity.Resume
import com.neptune.imapp.repository.entity.User
import com.neptune.imapp.repository.vo.SelfInfo
import com.neptune.imapp.ui.compose.screen.frontPage.register.Email
import com.neptune.imapp.ui.compose.screen.frontPage.register.Sex
import kotlinx.coroutines.launch

@Composable
fun MySelf(user: User, telephone: String) {
    Column(
        modifier = Modifier
            .fillMaxHeight()
            .padding(start = 10.dp, end = 10.dp, top = 20.dp, bottom = 80.dp),
        verticalArrangement = Arrangement.spacedBy(30.dp)
    ) {
        SelfCard(
            username = user.selfInfo.username,
            user.selfInfo.applyAmount,
            user.selfInfo.viewNum,
            user.selfInfo.focus,
            user.selfInfo.interActNum,
            user.selfInfo.sex
        )
        Resume(user.selfInfo, user.resume, telephone = telephone)
    }
}

@Composable
fun SelfCard(
    username: String,
    applyAmount: Int,
    viewNum: Int,
    focus: Int,
    interActNum: Int,
    sex: Int
) {
    val item = listOf("投递", "足迹", "关注", "互动")
    val num = listOf(applyAmount, viewNum, focus, interActNum)
    Surface(
        modifier = Modifier
            .wrapContentHeight()
            .fillMaxWidth(),
        shadowElevation = 10.dp,
        shape = RoundedCornerShape(5.dp)
    ) {
        Column(modifier = Modifier.padding(10.dp)) {
            Row(
                horizontalArrangement = Arrangement.Start,
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(bottom = 20.dp)
            ) {
                Image(
                    painter = painterResource(id = if (sex > 0) R.drawable.ic_boy else R.drawable.ic_girl),
                    contentDescription = null,
                    modifier = Modifier
                        .clip(
                            RoundedCornerShape(10.dp)
                        )
                        .size(80.dp)
                        .align(Alignment.CenterVertically)
                        .padding(start = 15.dp)
                )
                Text(
                    text = username,
                    fontSize = 30.sp,
                    modifier = Modifier
                        .align(Alignment.CenterVertically)
                        .padding(start = 10.dp)
                )
            }
            Row {
                item.forEachIndexed { index, s ->
                    Label(title = s, num = num[index], modifier = Modifier.weight(1.0f, true))
                }
            }
        }
    }
}

@OptIn(ExperimentalPagerApi::class)
@Composable
fun Resume(selfInfo: SelfInfo, resume: Resume, telephone: String) {
    val items = listOf("个人信息", "工作经历", "教育经历", "专业技能", "获得证书", "项目经历")
    var any: Any
    val state = remember { mutableStateOf(0) }
    val paperState = rememberPagerState(0)
    val scope = rememberCoroutineScope()
    LaunchedEffect(paperState) {
        snapshotFlow { paperState.currentPage }.collect {
            state.value = it
        }
    }
    Row(
        modifier = Modifier
            .fillMaxWidth(),
        horizontalArrangement = Arrangement.spacedBy(2.dp)
    ) {
        Surface(
            shadowElevation = 5.dp,
            modifier = Modifier.padding(5.dp),
            shape = RoundedCornerShape(10.dp)
        ) {
            Column(
                modifier = Modifier
                    .padding(horizontal = 10.dp, vertical = 10.dp)
                    .wrapContentWidth()
                    .fillMaxHeight(),
                verticalArrangement = Arrangement.spacedBy(10.dp)
            ) {
                Text(
                    text = "在线简历",
                    fontSize = 21.sp,
                    fontFamily = FontFamily.Monospace,
                )
                items.forEachIndexed { index, it ->
                    val scale =
                        animateFloatAsState(targetValue = if (state.value == index) 1.2f else 1.0f)
                    Surface(
                        shape = RoundedCornerShape(10.dp),
                        shadowElevation = 2.dp,
                        modifier = Modifier
                            .clickable(
                                interactionSource = MutableInteractionSource(),
                                indication = null
                            ) {
                                state.value = index
                                scope.launch {
                                    paperState.animateScrollToPage(index)
                                }
                            }
                            .scale(scale.value)
                            .weight(1.0f)
                    ) {
                        Row {
                            Text(
                                text = it,
                                modifier = Modifier
                                    .padding(10.dp)
                                    .align(Alignment.CenterVertically),
                                fontSize = 15.sp,
                                fontWeight = FontWeight.Bold,
                                color = MaterialTheme.colorScheme.primary
                            )
                        }
                    }
                }
            }
        }
        val dialog = remember {
            mutableStateOf(-1)
        }
        val dialogTitle = remember {
            mutableStateOf("")
        }
        SelfDialog(title = dialogTitle.value, dialog, selfInfo, resume)
        VerticalPager(
            count = items.size,
            state = paperState,
            itemSpacing = 30.dp,
            modifier = Modifier.padding(5.dp)
        ) {
            any = when (it) {
                0 -> selfInfo
                1 -> resume.workExperience.split("|")
                2 -> resume.education.split("|")
                3 -> resume.skill.split("|")
                4 -> resume.certification.split("|")
                5 -> resume.temp.split("|")
                else -> {}
            }
            ResumeLabel(any = any, type = it, dialogTitle, dialog, telephone)
        }
    }
}

@Composable
fun Label(title: String, num: Int, modifier: Modifier) {
    Column(horizontalAlignment = Alignment.CenterHorizontally, modifier = Modifier.then(modifier)) {
        Text(text = title, fontWeight = FontWeight.Bold)
        Text(text = num.toString())
    }
}

@OptIn(ExperimentalPagerApi::class)
@Composable
fun ResumeLabel(
    any: Any,
    type: Int,
    dialogTitle: MutableState<String>,
    dialog: MutableState<Int>,
    telephone: String
) {
    Surface(
        modifier = Modifier
            .padding(3.dp),
        color = MaterialTheme.colorScheme.primaryContainer,
        shape = RoundedCornerShape(10.dp),
        shadowElevation = 5.dp
    ) {
        Column(
            modifier = Modifier
                .fillMaxHeight()
                .padding(5.dp)
        ) {
            when (any) {
                is SelfInfo -> {
                    ResumeItem(
                        title = "姓名",
                        str = any.username,
                        Modifier.weight(1.0f)
                    )
                    ResumeItem(
                        title = "性别", str = if (any.sex > 0) "男" else "女",
                        Modifier.weight(1.0f)
                    )
                    ResumeItem(
                        title = "年龄", str = any.birthDay,
                        Modifier.weight(1.0f)
                    )
                    ResumeItem(
                        title = "邮箱", str = any.email,
                        Modifier.weight(1.0f)
                    )
                    ResumeItem(
                        title = "手机号", str = telephone,
                        Modifier.weight(1.0f)
                    )
                    Row(
                        modifier = Modifier
                            .fillMaxHeight()
                            .wrapContentHeight(Alignment.Bottom)
                    ) {
                        Surface(
                            shadowElevation = 1.dp,
                            shape = RoundedCornerShape(
                                bottomStart = 10.dp,
                                bottomEnd = 10.dp
                            )
                        ) {
                            Row(
                                modifier = Modifier
                                    .fillMaxWidth()
                                    .wrapContentWidth(Alignment.CenterHorizontally)
                                    .padding(8.dp)
                                    .clickable(
                                        interactionSource = MutableInteractionSource(),
                                        indication = null
                                    ) {
                                        dialog.value = 0
                                        dialogTitle.value = "修改基本信息"
                                    }
                            ) {
                                Text(text = "修改", fontSize = 20.sp)
                                Icon(
                                    painter = painterResource(id = R.drawable.baseline_add_circle_outline_24),
                                    contentDescription = null,
                                    modifier = Modifier.size(30.dp)
                                )
                                Text(text = "信息", fontSize = 20.sp)
                            }
                        }
                    }
                }
                is List<*> -> {
                    if (any.size > 0) {
                        when (type) {
                            1 -> {
                                Column {
                                    HorizontalPager(
                                        count = any.size
                                    ) {
                                        val workExperience = (any[it] as String).split("-")
                                        Column(verticalArrangement = Arrangement.Top) {
                                            ResumeItem(
                                                title = "公司名称",
                                                str = workExperience[0],
                                                Modifier.weight(1.0f)
                                            )
                                            ResumeItem(
                                                title = "所属行业",
                                                str = workExperience[0],
                                                Modifier.weight(1.0f)
                                            )
                                            ResumeItem(
                                                title = "职位名称",
                                                str = workExperience[0],
                                                Modifier.weight(1.0f)
                                            )
                                            ResumeItem(
                                                title = "工作描述",
                                                str = workExperience[0],
                                                Modifier.weight(1.0f)
                                            )
                                        }
                                    }
                                    Row(
                                        modifier = Modifier
                                            .fillMaxHeight()
                                            .wrapContentHeight(Alignment.Bottom)
                                    ) {
                                        Surface(
                                            shadowElevation = 1.dp,
                                            shape = RoundedCornerShape(
                                                bottomStart = 10.dp,
                                                bottomEnd = 10.dp
                                            )
                                        ) {
                                            Row(
                                                modifier = Modifier
                                                    .fillMaxWidth()
                                                    .wrapContentWidth(Alignment.CenterHorizontally)
                                                    .padding(8.dp)
                                                    .clickable(
                                                        interactionSource = MutableInteractionSource(),
                                                        indication = null
                                                    ) {
                                                        dialog.value = 1
                                                        dialogTitle.value = "新增工作经历"
                                                    }
                                            ) {
                                                Text(text = "新增请", fontSize = 20.sp)
                                                Icon(
                                                    painter = painterResource(id = R.drawable.baseline_add_circle_outline_24),
                                                    contentDescription = null,
                                                    modifier = Modifier.size(30.dp)
                                                )
                                                Text(text = "点此处", fontSize = 20.sp)
                                            }
                                        }
                                    }
                                }
                            }
                            2 -> {
                                Column {
                                    HorizontalPager(count = any.size) {
                                        val education = (any[it] as String).split("-")
                                        Column {
                                            ResumeItem(
                                                title = "学校名", str = education[0],
                                                Modifier.weight(1.0f)
                                            )
                                            ResumeItem(
                                                title = "专业",
                                                str = education[1],
                                                Modifier.weight(1.0f)
                                            )
                                            ResumeItem(
                                                title = "学位", str = education[2],
                                                Modifier.weight(1.0f)
                                            )
                                        }
                                    }
                                    Row(
                                        modifier = Modifier
                                            .fillMaxHeight()
                                            .wrapContentHeight(Alignment.Bottom)
                                    ) {
                                        Surface(
                                            shadowElevation = 1.dp,
                                            shape = RoundedCornerShape(
                                                bottomStart = 10.dp,
                                                bottomEnd = 10.dp
                                            )
                                        ) {
                                            Row(
                                                modifier = Modifier
                                                    .fillMaxWidth()
                                                    .wrapContentWidth(Alignment.CenterHorizontally)
                                                    .padding(8.dp)
                                                    .clickable(
                                                        interactionSource = MutableInteractionSource(),
                                                        indication = null
                                                    ) {
                                                        dialog.value = 2
                                                        dialogTitle.value = "新增学历"
                                                    }
                                            ) {
                                                Text(text = "新增请", fontSize = 20.sp)
                                                Icon(
                                                    painter = painterResource(id = R.drawable.baseline_add_circle_outline_24),
                                                    contentDescription = null,
                                                    modifier = Modifier.size(30.dp)
                                                )
                                                Text(text = "点此处", fontSize = 20.sp)
                                            }
                                        }
                                    }
                                }

                            }
                            3 -> {
                                Column {
                                    HorizontalPager(count = any.size) {
                                        val skill = (any[it] as String).split("-")
                                        Column {
                                            ResumeItem(
                                                title = "专业技能", str = skill[0],
                                                Modifier.weight(1.0f)
                                            )
                                            ResumeItem(
                                                title = "使用时间(月)",
                                                str = skill[1],
                                                Modifier.weight(1.0f)
                                            )
                                            ResumeItem(
                                                title = "掌握程度", str = skill[2],
                                                Modifier.weight(1.0f)
                                            )
                                        }
                                    }
                                    Row(
                                        modifier = Modifier
                                            .fillMaxHeight()
                                            .wrapContentHeight(Alignment.Bottom)
                                    ) {
                                        Surface(
                                            shadowElevation = 1.dp,
                                            shape = RoundedCornerShape(
                                                bottomStart = 10.dp,
                                                bottomEnd = 10.dp
                                            )
                                        ) {
                                            Row(
                                                modifier = Modifier
                                                    .fillMaxWidth()
                                                    .wrapContentWidth(Alignment.CenterHorizontally)
                                                    .padding(8.dp)
                                                    .clickable(
                                                        interactionSource = MutableInteractionSource(),
                                                        indication = null
                                                    ) {
                                                        dialog.value = 3
                                                        dialogTitle.value = "新增专业技能"
                                                    }
                                            ) {
                                                Text(text = "新增请", fontSize = 20.sp)
                                                Icon(
                                                    painter = painterResource(id = R.drawable.baseline_add_circle_outline_24),
                                                    contentDescription = null,
                                                    modifier = Modifier.size(30.dp)
                                                )
                                                Text(text = "点此处", fontSize = 20.sp)
                                            }
                                        }
                                    }
                                }

                            }
                            4 -> {
                                Column {
                                    HorizontalPager(count = any.size) {
                                        val certification = (any[it] as String).split("-")
                                        Column {
                                            ResumeItem(
                                                title = "证书名称", str = certification[0],
                                                Modifier.weight(1.0f)
                                            )
                                            ResumeItem(
                                                title = "获得时间",
                                                str = certification[1],
                                                Modifier.weight(1.0f)
                                            )
                                        }
                                    }
                                    Row(
                                        modifier = Modifier
                                            .fillMaxHeight()
                                            .wrapContentHeight(Alignment.Bottom)
                                    ) {
                                        Surface(
                                            shadowElevation = 1.dp,
                                            shape = RoundedCornerShape(
                                                bottomStart = 10.dp,
                                                bottomEnd = 10.dp
                                            )
                                        ) {
                                            Row(
                                                modifier = Modifier
                                                    .fillMaxWidth()
                                                    .wrapContentWidth(Alignment.CenterHorizontally)
                                                    .padding(8.dp)
                                                    .clickable(
                                                        interactionSource = MutableInteractionSource(),
                                                        indication = null
                                                    ) {
                                                        dialog.value = 4
                                                        dialogTitle.value = "新增学历证书"
                                                    }
                                            ) {
                                                Text(text = "新增请", fontSize = 20.sp)
                                                Icon(
                                                    painter = painterResource(id = R.drawable.baseline_add_circle_outline_24),
                                                    contentDescription = null,
                                                    modifier = Modifier.size(30.dp)
                                                )
                                                Text(text = "点此处", fontSize = 20.sp)
                                            }
                                        }
                                    }
                                }

                            }
                            5 -> {
                                Column {
                                    HorizontalPager(count = any.size) {
                                        val temp = (any[it] as String).split("-")
                                        Column {
                                            ResumeItem(
                                                title = "项目名", str = temp[0],
                                                Modifier.weight(1.0f)
                                            )
                                            ResumeItem(
                                                title = "开始时间",
                                                str = temp[1],
                                                Modifier.weight(1.0f)
                                            )
                                            ResumeItem(
                                                title = "结束时间",
                                                str = temp[2],
                                                Modifier.weight(1.0f)
                                            )
                                            ResumeItem(
                                                title = "项目描述", str = temp[3],
                                                Modifier.weight(1.0f)
                                            )
                                        }
                                    }
                                    Row(
                                        modifier = Modifier
                                            .fillMaxHeight()
                                            .wrapContentHeight(Alignment.Bottom)
                                    ) {
                                        Surface(
                                            shadowElevation = 1.dp,
                                            shape = RoundedCornerShape(
                                                bottomStart = 10.dp,
                                                bottomEnd = 10.dp
                                            )
                                        ) {
                                            Row(
                                                modifier = Modifier
                                                    .fillMaxWidth()
                                                    .wrapContentWidth(Alignment.CenterHorizontally)
                                                    .padding(8.dp)
                                                    .clickable(
                                                        interactionSource = MutableInteractionSource(),
                                                        indication = null
                                                    ) {
                                                        dialog.value = 5
                                                        dialogTitle.value = "新增项目经历"
                                                    }
                                            ) {
                                                Text(text = "新增请", fontSize = 20.sp)
                                                Icon(
                                                    painter = painterResource(id = R.drawable.baseline_add_circle_outline_24),
                                                    contentDescription = null,
                                                    modifier = Modifier.size(30.dp)
                                                )
                                                Text(text = "点此处", fontSize = 20.sp)
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    } else {
                        Column(
                            modifier = Modifier
                                .fillMaxWidth()
                                .fillMaxHeight()
                                .wrapContentSize(
                                    Alignment.Center
                                )
                        ) {
                            Icon(
                                painter = painterResource(id = R.drawable.baseline_add_circle_outline_24),
                                contentDescription = null,
                                modifier = Modifier
                                    .size(100.dp)
                                    .clickable(
                                        interactionSource = MutableInteractionSource(),
                                        indication = null
                                    ) {
                                        dialog.value = type
                                        when (type) {
                                            1 -> {
                                                dialogTitle.value = "新增工作经历"
                                            }
                                            2 -> {
                                                dialogTitle.value = "新增学历"
                                            }
                                            3 -> {
                                                dialogTitle.value = "新增专业技能"
                                            }
                                            4 -> {
                                                dialogTitle.value = "新增证书"
                                            }
                                            5 -> {
                                                dialogTitle.value = "新增项目"
                                            }
                                        }
                                    },
                                tint = MaterialTheme.colorScheme.surfaceTint
                            )
                        }
                    }
                }
            }
        }
    }
}

@Composable
fun ResumeItem(title: String, str: String, modifier: Modifier) {
    Surface(
        modifier = Modifier
            .padding(4.dp)
            .clip(RoundedCornerShape(10.dp))
            .wrapContentHeight()
    ) {
        Column(
            verticalArrangement = Arrangement.SpaceAround,
            modifier = Modifier
                .padding(3.dp)
                .fillMaxWidth()
                .wrapContentHeight(align = Alignment.CenterVertically)
                .then(modifier)
        ) {
            Text(
                text = title,
                fontSize = 17.sp,
                modifier = Modifier.padding(top = 10.dp, start = 5.dp, bottom = 10.dp),
                color = MaterialTheme.colorScheme.primary
            )
            Text(text = str, fontSize = 20.sp, modifier = Modifier.padding(bottom = 10.dp))
        }
    }
}

@Composable
fun SelfDialog(title: String, dialog: MutableState<Int>, selfInfo: SelfInfo, resume: Resume) {
    AnimatedVisibility(visible = dialog.value >= 0, enter = fadeIn(), exit = fadeOut()) {
        Dialog(onDismissRequest = { dialog.value = -1 }) {
            Surface(shape = RoundedCornerShape(10.dp)) {
                Column(
                    verticalArrangement = Arrangement.spacedBy(10.dp),
                    horizontalAlignment = Alignment.CenterHorizontally,
                    modifier = Modifier.padding(30.dp)
                ) {
                    Text(
                        text = title,
                        fontSize = 20.sp,
                        modifier = Modifier
                            .fillMaxWidth()
                            .wrapContentWidth(Alignment.CenterHorizontally)
                    )
                    InputForm(dialog.value, selfInfo, resume)
                }
            }
        }
    }
}

@Composable
fun InputForm(type: Int, selfInfo: SelfInfo, resume: Resume) {
    when (type) {
        0 -> {
            val email = remember {
                mutableStateOf(selfInfo.email)
            }
            val username = remember {
                mutableStateOf(selfInfo.username)
            }
            val sex = remember {
                mutableStateOf(0)
            }
            BasicTextField(
                value = username.value,
                onValueChange = { username.value = it },
                singleLine = true,
                modifier = Modifier.fillMaxWidth(),
                textStyle = TextStyle(textAlign = TextAlign.Center, fontSize = 20.sp),
                decorationBox = @Composable { innerTextField ->
                    if (username.value.isEmpty())
                        Text(
                            text = "请输入真实姓名",
                            modifier = Modifier.fillMaxWidth(),
                            textAlign = TextAlign.Center,
                            fontSize = 20.sp,
                            color = Color.Gray
                        )
                    innerTextField()
                })
            Email(email)
            Sex(sex = sex)
        }
        1 -> {
            val companyName = remember {
                mutableStateOf("")
            }
            val vacation = remember {
                mutableStateOf("")
            }
            val position = remember {
                mutableStateOf("")
            }
            val introduction = remember {
                mutableStateOf("")
            }

            OutlinedTextField(
                value = companyName.value,
                onValueChange = { companyName.value = it },
                placeholder = {
                    Text(
                        text = "请输入公司名"
                    )
                })
            OutlinedTextField(
                value = vacation.value,
                onValueChange = { vacation.value = it },
                placeholder = {
                    Text(
                        text = "请输入公司名"
                    )
                })
            OutlinedTextField(
                value = position.value,
                onValueChange = { position.value = it },
                placeholder = {
                    Text(
                        text = "请输入公司名"
                    )
                })
            OutlinedTextField(
                value = introduction.value,
                onValueChange = { introduction.value = it },
                placeholder = {
                    Text(
                        text = "请输入公司名"
                    )
                })
        }
    }
}
